package com.example.yichuangyun.mvvm.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.lifecycle.Observer;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivitySearchCompanyBinding;
import com.example.yichuangyun.model.company.CompanyDataModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.SearchNearbyCompanyAdapter;
import com.example.yichuangyun.mvvm.ui.fragment.view.SearchCompanyViewModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.SearchHistoryUtil;
import com.example.yichuangyun.utils.ToastHelper;
import com.example.yichuangyun.widget.TagContainerLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业搜索
 */
public class SearchCompanyActivity extends BaseActivity<ActivitySearchCompanyBinding, SearchCompanyViewModel> {

    /**
     * 0企业列表  1地图
     */
    private int flag;
    private SearchNearbyCompanyAdapter adapter;

    @Override
    protected SearchCompanyViewModel bindModel() {
        return getViewModel(this, SearchCompanyViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        initTitleBar(getString(R.string.search));
        initHistory();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            flag = bundle.getInt(Constant.flagKey, 0);
        }
        if (flag == 0) {
            String keywords = getIntent().getExtras().getString(Constant.keywordsKey);
            if (!TextUtils.isEmpty(keywords)) {
                binding.etSearchContent.setText(keywords);
                binding.ivClear.setVisibility(View.VISIBLE);
            }
        } else {
            adapter = new SearchNearbyCompanyAdapter(context);
            binding.recyclerView.setAdapter(adapter);
        }
    }

    private void getCompanyList() {
        String keywords = binding.etSearchContent.getText().toString().trim();
        adapter.setKeywords(keywords);
        showLoadingDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", 20);
        map.put("keywords", keywords);
        viewModel.getCompanyList(map);
    }

    /**
     * 搜索历史
     */
    private void initHistory() {
        final List<String> data = SearchHistoryUtil.getHistory();
        binding.llSearchHistory.setVisibility(View.VISIBLE);
        binding.tagLayout.setTags(data);
        binding.tagLayout.setOnTagListener(new TagContainerLayout.TagListener() {
            @Override
            public void onTagClick(int position) {
                binding.etSearchContent.setText(data.get(position));
                search();
            }

            @Override
            public void removeTag(int position) {

            }
        });
    }


    @Override
    protected void initClick() {
        binding.tvSearch.setOnClickListener(view -> search());

        binding.ivClearHistory.setOnClickListener(view -> {
            SearchHistoryUtil.cleanHistory();
            initHistory();
        });

        binding.etSearchContent.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                search();
                return true;
            }
            return false;
        });

        binding.etSearchContent.setOnTextChangeListener(text ->
                binding.ivClear.setVisibility(TextUtils.isEmpty(text.toString()) ? View.GONE : View.VISIBLE));

        binding.ivClear.setOnClickListener(view -> {
            binding.etSearchContent.setText("");
            binding.ivClear.setVisibility(View.GONE);
        });

        // 企业列表返回结果
        viewModel.companyDataModelMutableLiveData.observe(this, companyDataModel -> {
            List<CompanyDataModel.CompanyModel> data = companyDataModel.getData();
            adapter.submitList(data);
            binding.recyclerView.setVisibility(null != companyDataModel.getData() && companyDataModel.getData().size() > 0 ? View.VISIBLE : View.GONE);
            dismissLoadingDialog();
        });

        viewModel.getListErrorMutableLiveData.observe(this, s -> dismissLoadingDialog());
    }

    private void search() {
        if (TextUtils.isEmpty(binding.etSearchContent.getText().toString().trim())) {
            ToastHelper.show(getString(R.string.please_input_search_company_content));
        } else {
            CommonUtil.closeKeyboard(this);
            String keywords = binding.etSearchContent.getText().toString().trim();
            SearchHistoryUtil.saveKeyWord(keywords);
            if (flag == 0) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString(Constant.keywordsKey, keywords);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            } else {
                getCompanyList();
            }
        }
    }
}